package com.zeratul.demo.service;

import com.zeratul.common.cache.annotation.ZtCacheEvict;
import com.zeratul.common.cache.annotation.ZtCacheable;
import com.zeratul.common.lock.annotation.ZtLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class DemoServiceImpl {

    @Autowired
    private DemoServiceImpl2 demoService;

    @Async
    public void demoService(){
        log.info("测试Service");
//        System.out.println(1/0);
    }

    @ZtCacheable(key = "#key", timeout = 30, unit = TimeUnit.SECONDS)
    public String getService(String key){
        System.out.println("查询业务方法执行了！");
        return switch (key) {
            case "a" -> "张三";
            case "b" -> "李四";
            case "c" -> "王五";
            default -> "default";
        };
    }

    /**
     * 删除缓存
     */
    @ZtCacheEvict(key = "#key")
    @ZtLock(key = "#key")
    public String updateService(String key){
        System.out.println("修改业务方法执行了！");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "修改成功";
    }

    public String fallback(String key){
        System.out.println("降级方法！");
        return "OK";
    }
}
